### Disjoint unions

class UnionFind(object):
	class _node(object):
		def __init__(self,i):
			self.parent=self
			self.rank=0
			self.id=i
	def __init__(self,objs):
		"""Union-find of objs.  Objects in objs must be hashable."""
		self._nodes={}
		for o in objs:
			self._nodes[o]=self._node(o)
	def union(self,x,y):
		x=self._find(self._nodes[x])
		y=self._find(self._nodes[y])
		if x.rank > y.rank:
			y.parent = x
		elif x.rank < y.rank:
			x.parent = y
		elif x.id != y.id:
			y.parent = x
			x.rank += 1
	def find(self,x):
		return self._find(self._nodes[x]).id
	def _find(self,x):
		if x.parent==x:
			return x
		else:
			x.parent=self._find(x.parent)
			return x.parent
	